/***************************************************************************
  A SymbolBrowser plugin for JuffEd editor.

  Copyright: 2010 Aleksander Sokoloff <asokol@mail.ru>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License.
  version 2 as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have rceived a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
***************************************************************************/

#include "items.h"


#include <QIcon>
#include <QDebug>

//#define DEBUG_TREE
/**********************************************************
 *
 *
 **********************************************************/
Item::Item(const QString & text):
    QStandardItem(text)
{
    mDetailed = true;
}


/**********************************************************
 *
 **********************************************************/
Item::Item(const Symbol *symbol):
    SymbolInfo(symbol),
    QStandardItem()
{
    mDetailed = false;
    setEditable(false);

    if (symbol->relatedSymbol())
        mRelatedSymbol = new SymbolInfo(symbol->relatedSymbol());
    else
        mRelatedSymbol = NULL;
}


/**********************************************************
 *
 **********************************************************/
void Item::setDetailed(bool detailed)
{
    mDetailed = detailed;
    updateText();

    int cnt = rowCount();
    for (int i=0; i<cnt; ++i)
    {
        Item *item = dynamic_cast<Item*>(child(i, 0));
        if (item) item->setDetailed(detailed);
    }
}


/**********************************************************
 *
 **********************************************************/
void  Item::updateText()
{
#ifdef DEBUG_TREE
    if (mDetailed && !mDetailedText.isEmpty())
        setText(QString("%1 %2").arg(mLine).arg(mDetailedText));
    else
        setText(QString("%1 %2").arg(mLine).arg(mName));
#else
    if (mDetailed && !mDetailedText.isEmpty())
        setText(mDetailedText);
    else
        setText(mName);
#endif
}


/**********************************************************
 *
 **********************************************************/
void Item::setPath(const QString &path)
{
    mPath = path;
}


/**********************************************************
 *
 **********************************************************/
bool Item::operator< (const QStandardItem & other) const
{
    Item const *o = dynamic_cast<const Item*>(&other);
    if (!o) return true;

    switch (model()->sortRole())
    {
        case Qt::UserRole + SortByName: return this->name() < o->name();
        case Qt::UserRole + SortByLine: return this->line() < o->line();
        default: return true;
    }
}



/**********************************************************
 *
 *
 **********************************************************/
ItemClass::ItemClass(const Symbol *symbol): Item(symbol)
{
    setIcon(QIcon(":icon_class"));
}



/**********************************************************
 *
 *
 **********************************************************/
ItemStruct::ItemStruct(const Symbol *symbol): Item(symbol)
{
    setIcon(QIcon(":icon_class"));
}



/**********************************************************
 *
 *
 **********************************************************/
ItemNamespace::ItemNamespace(const Symbol *symbol): Item(symbol)
{
    setIcon(QIcon(":icon_namespace"));
}



/**********************************************************
 *
 *
 **********************************************************/
ItemFunc::ItemFunc(const Symbol *symbol): Item(symbol)
{
    setIcon(QIcon(":icon_func"));
}



/**********************************************************
 *
 *
 **********************************************************/
ItemPrototype::ItemPrototype(const Symbol *symbol): Item(symbol)
{
    setIcon(QIcon(":icon_func"));
}


